package org.korecky.gnotedigitalorganizer;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.korecky.gnotedigitalorganizer.data.Book;
import org.korecky.gnotedigitalorganizer.data.Page;
import org.korecky.gnotedigitalorganizer.settings.DeviceSettings;
import org.korecky.gnotedigitalorganizer.settings.DeviceSettingsHelper;

public class DeviceManager {

    /**
     * Pair G-Note device with G-Note Digital Organizer application
     * 
     * @param deviceRoot
     * @return
     * @throws Exception
     */
    public static UUID pairDeviceWihApplication(File deviceRoot, DeviceSettings deviceSettings) throws Exception {
	UUID deviceID = null;
	File deviceSettingsFile = new File(deviceRoot.getAbsolutePath() + System.getProperty("file.separator") + ".gnotedigitalorganizer");
	if (deviceSettingsFile.exists()) {
	    DeviceSettings originalDeviceSettings = DeviceSettingsHelper.loadDeviceSettings(deviceSettingsFile);
	    deviceSettings.setId(originalDeviceSettings.getId());
	}
	DeviceSettingsHelper.saveDeviceSettings(deviceSettingsFile, deviceSettings);
	deviceID = UUID.fromString(deviceSettings.getId());
	return deviceID;
    }

    /**
     * Checks if a device is pair with the application
     * 
     * @param deviceRoot
     * @return
     * @throws Exception
     */
    public static boolean isDevicePairWihApplication(File deviceRoot) throws Exception {
	File deviceSettingsFile = new File(deviceRoot.getAbsolutePath() + System.getProperty("file.separator") + ".gnotedigitalorganizer");
	if (deviceSettingsFile.exists()) {
	    DeviceSettings deviceSettings = DeviceSettingsHelper.loadDeviceSettings(deviceSettingsFile);
	    if ((deviceSettings.getId() != null) && (deviceSettings.getId().length() > 0))
		return true;
	}
	return false;
    }

    /**
     * Gets all books from G-Note device
     * 
     * @param directory
     * @return
     */
    public static List<Book> getBooksFromDevice(File directory) {
	List<String> dntFiles = new ArrayList<String>();
	for (String filePath : directory.list()) {
	    File file = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + filePath);
	    if (!file.isDirectory()) {
		int dot = file.getAbsolutePath().lastIndexOf(".");
		String currentFileExtension = file.getAbsolutePath().substring(dot + 1);
		if (currentFileExtension.toLowerCase().equals("dnt")) {
		    dntFiles.add(filePath);
		}
	    }
	}
	// Sort files
	Collections.sort(dntFiles);
	// Create collection of Books and Pages
	ArrayList<Book> books = new ArrayList<Book>();
	Book previousBook = new Book();
	for (String dntFile : dntFiles) {
	    int lastSeparator = dntFile.lastIndexOf("-");
	    String bookName = dntFile.substring(0, lastSeparator);
	    String pageName = dntFile.substring(lastSeparator + 1, dntFile.length());
	    if (!StringUtils.equalsIgnoreCase(bookName, previousBook.getTitle())) {
		Book newBook = new Book();
		newBook.setTitle(bookName);
		newBook.setPages(new ArrayList<Page>());
		previousBook = newBook;
		books.add(newBook);
	    }
	    Page newPage = new Page();
	    newPage.setTitle(pageName);
	    newPage.setUrl(directory.getAbsolutePath() + System.getProperty("file.separator") + dntFile);
	    previousBook.getPages().add(newPage);
	}
	return books;
    }
}
